<template>
    <view class="main-admin bg-admin">
        <view class="login shadow">
            <view class="pic&quot;"><text class="icon-settingsfill text-orange"></text></view>
            <view class="hint">后台管理系统</view>

            <view class="form-group margin-top">
                <view class="title">账号：</view>
                <input placeholder="请输入管理员账号" maxlength="30" placeholder-class="phc" v-model="name" />
            </view>

            <view class="form-group">
                <view class="title">密码：</view>
                <input placeholder="请输入管理员密码" maxlength="30" type="password" placeholder-class="phc" v-model="pwd" />
            </view>
            <view class="form-group-pwd" @tap="bindRememberTap">
                <text :class="remember ? 'icon-squarecheckfill text-blue' : 'icon-square'"></text>
                记住密码
            </view>

            <button class="btn bg-admin margin-bottom" @tap="bindLoginTap">点击登录</button>
            <view class="return text-grey">本系统仅限于系统管理员登录</view>
            <view @tap="bindBackTap" class="return text-grey">
                <text class="icon-back"></text>
                返回用户端
            </view>
        </view>

        <view class="foot-bottom">
            <cmpt-foot />
        </view>
    </view>
</template>

<script>
import cmptFoot from '@/cmpts/biz/foot/foot_cmpt';
import AdminBiz from '@/utils/comm/biz/admin_biz.js';
import pageHelper from '@/utils/helper/page_helper.js';
import cacheHelper from '@/utils/helper/cache_helper.js';
export default {
    components: {
        cmptFoot
    },
    data() {
        return {
            name: '',
            pwd: '',
            remember: false
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        AdminBiz.clearAdminToken();

        // 记住密码
        let pwd = cacheHelper.get('admin-pwd');
        if (pwd) {
            this.setData({
                name: pwd.name,
                pwd: pwd.pwd,
                remember: true
            });
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    methods: {
        url: function (e) {
            pageHelper.url(e, this);
        },

        bindBackTap: function (e) {
            uni.reLaunch({
                url: pageHelper.fmtURLByPID('/pages/my/index/my_index')
            });
        },

        bindLoginTap: async function (e) {
            // 记住密码
            if (this.remember) {
                cacheHelper.set(
                    'admin-pwd',
                    {
                        pwd: this.pwd,
                        name: this.name
                    },
                    2592000
                );
            } else {
                cacheHelper.remove('admin-pwd');
            }
            return AdminBiz.adminLogin(this, this.name, this.pwd);
        },

        bindRememberTap: function (e) {
            this.setData({
                remember: !this.remember
            });
        }
    }
};
</script>
<style>
@import './admin_login.css';
</style>
